package com.cyn.controller;

import com.cyn.aframework.annotation.Apple;
import com.cyn.aframework.annotation.FusionOperation;
import com.cyn.aframework.annotation.Valuee;
import com.cyn.aframework.http.HttpUtils;
import com.cyn.controller.dto.Test.Test;
import com.cyn.controller.dto.Test.Test2;
import com.cyn.controller.rabbitmq.HelloSender;
import com.cyn.entity.SysUser;
import com.cyn.service.SysUserService;
import com.cyn.service.TransactionalTestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@RestController
@RequestMapping("demo")
public class DemoController {

    @Autowired
    SysUserService sysUserService;

    @Value(value = "Apple")
    private String appleNames;

    @Valuee(valuee = "Apple2")
    private String appleNames2;

    @Autowired
    Apple apple;

    @Autowired
    private HelloSender helloSender;
    @Autowired
    private RequestMappingHandlerMapping handlerMapping;



    @GetMapping("requeMapLoaction")
    public Test requeMapLoaction() throws Exception {

        Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();

        return null;
    }


    /**
     * 死锁
     * @return
     */
    @GetMapping("/deadLock")
    public String deadLock(){
        Lock lock1=new ReentrantLock();
        //线程t1中
        new Thread(()->{
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            while (!lock1.tryLock()){
                System.out.println("线程1 一直自旋获取锁");
            }
            System.out.println("线程1 获取到锁 do something");
        }).start();

        // 线程2
        new Thread(()->{
            lock1.lock();  //此时线程2 获取到锁 但是一直阻塞 导致锁无法释放
            while (true){

            }
        }).start();

        return "Success";
    }




    @GetMapping("dddff")
    public Test dddfff() throws Exception {

     return null;
    }

    @GetMapping("ddgg")
    public Test2 ddgg() {
        Test2 test = new Test2();
        test.setAge(4);
        test.setName("张三");
        return test;
    }

    @Autowired
    TransactionalTestService transactionalTestService;
    private static Logger log = LoggerFactory.getLogger(LogBackTestController.class);

    @FusionOperation(mapping = "m", desc = "cs", method = RequestMethod.GET)
    public Map<String,Object> getApple(){
        Map<String, Object> params = new HashMap<>();
        params.put("appleName",appleNames);
//        params.put("appleNames2",appleNames2);
        System.out.println(false);
        return params;
    }


    @GetMapping("testk")
    public Map<String,Object> test111() {
        for (int i = 0; i < 10; i++) {
            helloSender.send(i);
        }

        return null;
    }

    @GetMapping("testk2")
    public Map<String,Object> testk2() {

        transactionalTestService.test();
        return null;
    }

    @GetMapping("test")
    public Map<String,Object> test() {
        System.out.println("test");
        Map<String, Object> params = new HashMap<>();
        try {
            InetAddress inetAddress = InetAddress.getLocalHost();
            String address = inetAddress.getAddress().toString();
            String hostAddress = inetAddress.getHostAddress();
            params.put("address",address);
            params.put("hostAddress",hostAddress);
            params.put("hostName",inetAddress.getHostName());
            params.put("canonicalHostName",inetAddress.getCanonicalHostName());
            return params;
        } catch (UnknownHostException e) {
            System.out.println(e);
        }
        return params;
    }

    @GetMapping("trantest")
    public String trantest() {
        String tmp = sysUserService.test();
        return tmp;
    }

    @GetMapping("test2")
    public String test2() {
        String tmp = sysUserService.test();
        return tmp;
    }
    @GetMapping("test3")
    public SysUser test3(HttpServletRequest request,String testname) {
        SysUser sysUser = new SysUser();
        sysUser.setSex(1);
        sysUser.setStatus(1);
        sysUser.setLoginName("123");
        SysUser tmp = sysUserService.insert(sysUser);

        log.error("*****************："+testname+"******"+tmp);

        return tmp;
    }

    @GetMapping("test43")
    public SysUser test43(HttpServletRequest request,String testname) {
        SysUser sysUser = new SysUser();
        String[] tmp = testname.split("_");
        sysUser.setUserName(tmp[0]);
        sysUser.setLoginName(tmp[1]);
        sysUser.setCreateTime(new Date());
        sysUser.setUpdateTime(new Date());
        sysUser.setSex(1);
        sysUser.setStatus(1);
        sysUserService.insert(sysUser);

        log.error("*****************："+testname+"******"+tmp);

        return sysUser;
    }

    @GetMapping("test4")
    public String test4() {
        String tmp = HttpUtils.sendGet("http://39.105.38.10:8081/book/info","id=1119522");
        return tmp;
    }


    @GetMapping("testrequestTime")
    public String testrequestTime() {
        Long beginTime = System.currentTimeMillis();
        Long end = 0l;
        while(end<=30000){
            end = System.currentTimeMillis()-beginTime;
            System.out.println(end);
        }
        return "fff";
    }

    @GetMapping("pooltest")
    public Map<String,Object> pooltest() {
        Map<String,Object> r = this.r();

        return r;
    }

    private Map<String,Object> r(){
        Map<String,Object> obj = new HashMap<>();
        ExecutorService pool = Executors.newCachedThreadPool();
        Future<Map<String,Object>> test = pool.submit(()->{
            obj.put("test1",123);
            Thread.sleep(3000);
            System.out.println("123");

            return obj;
        });

        Future<Map<String,Object>> test2 = pool.submit(()->{
            obj.put("test2",456);
            System.out.println("456");
            Thread.sleep(1000);
            return obj;
        });
        try {
            System.out.println("789");
            Map<String,Object> result1 = test.get();
            Map<String,Object> result2 = test2.get();
            Map<String,Object> obj1 = new HashMap<>();
            obj1.put("te1",result1);
            obj1.put("te2",result2);
            return obj1;
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }


    @GetMapping("thtest")
    public Integer thtest(){

        Integer i =0;
        Thread t = new Thread(()->{
            try {
                Thread.sleep(3000);
                System.out.println(i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t.start();
        return 1;
    }
}